import { apiAuditProjectList } from '../other/api'

import { regeneratorRuntime } from '../other/bundle'

const initState = {
  // UI
  uiSologan: '夫安装者，匠心传承',
  uiLogoBrand: '盘古之手',

  auditProjectList: {
    actionStatus: 'INIT',
    data: {}
  }
}

export const initHome = data => {
  return async dispatch => {
    await dispatch({ data, type: 'INIT_HOME', })
  }
}

export const getAuditProjectList = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditProjectList(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error(apiRes)
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'GET_AUDIT_PROJECT_LIST' })
    return data;
  }
}


export const reducer = (state = initState, action) => {
  switch (action.type) {
    case 'INIT_HOME':
      return {
        ...state,
        HOME_INIT: action.data
      }
      break;
    case 'GET_AUDIT_PROJECT_LIST':
      return {
        ...state,
        auditProjectList: action.data
      }
      break;
    default:
      return state
      break;
  }
}

export default { 
  reducer,
  initHome, 
  getAuditProjectList,
}